package com.xz.xzliving.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.xzliving.commodity.dao.CategoryDao;
import com.xz.xzliving.commodity.entity.CategoryEntity;
import com.xz.xzliving.commodity.service.CategoryService;
import com.xz.xzliving.common.utils.PageUtils;
import com.xz.xzliving.common.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author xuzhou
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    /**
     * 查询分类树形结构
     * java8 流式计算(stream api) + 递归操作
     *
     * @return
     */
    @Override
    public List<CategoryEntity> listTree() {
        // 1. 查出所有的分类数据
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        // 2. 组装成层级树形结构
        // 2.1 过滤返回一级分类
        // 2.2 进行 map 映射操作，给每个分类设置对应的子分类
        List<CategoryEntity> categoryTree = categoryEntities.stream().filter(categoryEntity -> categoryEntity.getParentId() == 0)
//                .map(category -> {
//                    category.setChildrenCategories(getChildrenCategories(category, categoryEntities));
//                    return category;
//                })
                .peek(category -> category.setChildrenCategories(getChildrenCategories(category, categoryEntities)))
//                .sorted((category1, category2) -> {
//                    return (category1.getSort() == null ? 0 : category1.getSort()) -
//                            (category2.getSort() == null ? 0 : category2.getSort());
//                })
                .sorted(Comparator.comparingInt(category -> (category.getSort() == null ? 0 : category.getSort())))
                .collect(Collectors.toList());

        return categoryTree;
    }

    /**
     * 递归查询所有分类的子分类
     *
     * @param root 一级分类
     * @param all  所有分类信息
     * @return
     */
    private List<CategoryEntity> getChildrenCategories(CategoryEntity root, List<CategoryEntity> all) {
        return all.stream()
                .filter(categoryEntity -> categoryEntity.getParentId().equals(root.getId()))
//                .map(categoryEntity -> {
//                    categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
//                    return categoryEntity;
//                })
                .peek(categoryEntity -> categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all)))
//                .sorted((categoryEntity1, categoryEntity2) -> {
//                    return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) -
//                            (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
//                })
                .sorted(Comparator.comparingInt(categoryEntity -> (categoryEntity.getSort() == null ? 0 : categoryEntity.getSort())))
                .collect(Collectors.toList());
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

}